﻿using System;
using System.Linq;
using System.Web;
using System.Windows.Forms;
using System.Xml;
using System.Text.RegularExpressions;
using System.Reflection;
namespace WinnerCMS.Common
{
    /// <summary>
    /// XML 的摘要说明。
    /// </summary>
    public class Xml
    {

        #region 保存Web.config
        /// <summary>
        /// 保存Web.config
        /// </summary>
        /// <param name="strKeyName">节点名</param>
        /// <param name="strKeyValue">要保存的值</param>
        /// <param name="strWhich">哪个配置文件</param>
        public static void SaveSetting(string strKeyName, string strKeyValue, string strWhich)
        {
            //创建XML文档实例
            XmlDocument xmlWebSetting = new XmlDocument();

            //打开XML文档
            xmlWebSetting.Load(HttpRuntime.AppDomainAppPath + "/" + strWhich);

            //查找节点所在位置
            XmlNodeList xmlNodeList = xmlWebSetting.SelectSingleNode("//appSettings").ChildNodes;

            foreach (XmlNode xn in xmlNodeList.Cast<XmlNode>().Where(xn => xn.Attributes != null && xn.Attributes["key"].InnerText == strKeyName))
            {
                if (xn.Attributes != null)
                {
                    xn.Attributes["value"].InnerText = strKeyValue;
                }
                xmlWebSetting.Save(HttpContext.Current.Server.MapPath("~/" + strWhich + ""));
                break;
            }
        }
        #endregion

        #region 读取 XML中指定节点值
        /// <summary>
        /// 读取 XML中指定节点值
        /// </summary>
        /// <param name="strKeyName"></param>
        /// <returns></returns>
        public static string GetConfig(string strKeyName)
        {
            return GetConfig(strKeyName, "Web.config");
        }

        /// <summary>
        /// 读取 XML中指定节点值
        /// </summary>
        /// <param name="strKeyName">节点键名</param>
        /// <param name="strWhich"></param>
        /// <returns>指定节点键值</returns>
        public static string GetConfig(string strKeyName, string strWhich)
        { 
            string configpath;
            try
            {
                configpath = string.Concat(HttpRuntime.AppDomainAppPath , @"\" , strWhich);

            }
            catch
            {
                configpath = Application.StartupPath;

                if (configpath.EndsWith(@"\bin"))
                {
                    configpath = configpath.Substring(0, configpath.Length - 4);
                }
                configpath = string.Concat(configpath, @"\", strWhich);
            }

            using (XmlTextReader tr = new XmlTextReader(configpath))
            {
                while (tr.Read())
                {
                    if (tr.NodeType == XmlNodeType.Element)
                    {
                        if (tr.Name == "add" && tr.GetAttribute("key") == strKeyName)
                        {
                            return tr.GetAttribute("value");
                        }
                    }
                }
            }
            return null;
        } 
        #endregion

         
    }
}